﻿using CSM_SQL;
using CSM_SQL.BLL;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CommunitySuperMarketManager.Controllers
{
    public class PersonalMsgController : Controller
    {
        private EmpBLL _EmpBll;

        private EmpBLL eb
        {
            get
            {
                if (_EmpBll == null)
                    _EmpBll = new EmpBLL();
                return _EmpBll;
            }
        }
        // GET: PersonalMsg
        public ActionResult PersonalOrders()
        {
            var e = (Emp)HttpContext.Session["CurrUser"];
            if (e != null)
            {
                ViewBag.CurrUser = e;
                return View();
            }
            return RedirectToAction("Login", "Login");
        }

        public void Completing(int ID)
        {
            //int ID;
            //var e = (Emp)HttpContext.Session["CurrUser"];
            //if (e != null)
            //{
            //    ID = e.E_ID;
            //}
            //else
            //{
            //    return;
            //}
            var pageNamber = Convert.ToInt32(Request.Params["page"]);
            var pageSize = Convert.ToInt32(Request.Params["rows"]);

            var list = eb.GetOrder(ID).Where(ord => ord.state < 3);
            Hashtable ht = new Hashtable
            {
               { "total", list.Count() },
               { "rows", list.Take((pageSize * pageNamber)).Skip(pageSize * (pageNamber - 1)).ToList<Emp_Ord>() }
            };
            HttpContext.Response.Write(JsonConvert.SerializeObject(ht));
            HttpContext.Response.End();
        }

        public void Send(int id)
        {
            if (eb.Send(id))
                HttpContext.Response.Write(0);
            else
                HttpContext.Response.Write(1);
            HttpContext.Response.End();
        }

        public void Select(int id)
        {
            var pageNamber = Convert.ToInt32(Request.Params["pageNumber"]);
            var pageSize = Convert.ToInt32(Request.Params["pageSize"]);
            var state = Convert.ToInt16(Request.Params["state"]);
            var selecttext = Request.Params["selecttext"];

            var list = eb.GetOrder(id);
            if (state != -1)
                list = (from l in list where l.state == state select l).ToList();
            else
                list = (from l in list where l.state>0&&l.state<3 select l).ToList();
            if (!string.IsNullOrEmpty(selecttext))
                list = (from l in list where l.goodsName.Contains(selecttext) || l.phone.Contains(selecttext)
                        ||l.customer.Contains(selecttext) || l.endTime.Contains(selecttext) || l.startTime.Contains(selecttext) 
                        || l.address.Contains(selecttext) || l.amount.ToString().Contains(selecttext)
                        select l).ToList();

            Hashtable ht = new Hashtable
            {
               { "total", list.Count() },
               { "rows", list.Take((pageSize * pageNamber)).Skip(pageSize * (pageNamber - 1)).ToList<Emp_Ord>() }
            };
            HttpContext.Response.Write(JsonConvert.SerializeObject(ht));
            HttpContext.Response.End();
        }
    }
}